package it.hopeless.jee6.injection;

import it.hopeless.jee6.logging.MyLogger;
import it.hopeless.jee6.security.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.enterprise.context.SessionScoped;
import javax.enterprise.inject.Produces;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;

/**
 * User: frico
 * Date: Feb 13, 2010
 * Time: 7:47:44 PM
 */

@SessionScoped
@Named
public class Login implements Serializable {
    @Inject @MyLogger
    Logger logger;

    @Inject
    private Credentials credentials;
    private User user;

    public void login() {
        user = retrieveUser(credentials.getUsername(), credentials.getPassword());
    }

    private User retrieveUser(String username, String password) {// fake method
        logger.debug("Retrieving User from injected credentials {}, {}", username, password);
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        return user;
    }

    public void logout() {
        user = null;
    }

    public boolean isLoggedIn() {
        return user != null;
    }

    @Produces
    @LoggedIn
    User getCurrentUser() {
        return user;
    }


}